<!--信号记录-->
<template>
  <div class="bg" ref="content" @viewappear="viewappear" @viewdisappear="viewdisappear">

    <VTips class="tips" v-if="datas && !datas.length" 
    :text="['无消息～']" image="list_kong.png" 
    :width="283" :height="238" />
    <template v-else>
      <list class="list" :loadmoreoffset="50" @loadmore="loadmore">

        <!--刷新-->
        <refresh class="refresh" @refresh="refresh" :display="isRefresh ? 'show' : 'hide'">
          <loading-indicator class="load text1"></loading-indicator>
        </refresh>

        <!--内容-->
        <cell v-for="(item, index) in datas" v-bind:key="index">

          <SignalRecordCell v-if="item.content" class="cell" :item="item" />

          <div class="genduo" v-else>
            <text class="font22 text3">{{T('没有更多数据')}}</text>
          </div>
        </cell>
      </list>
    </template>
  </div>
</template>

<script>

//组件

//功能
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import jconfig from "@/_js/config";
import jtime from "@/_js/time"
import jnavigator from "@/_js/navigator"; 
import jbroadcast from "@/_js/broadcast";
import jhttps from "@/_js/https";
import jSignal from "./view/Signal"

const weexFunction = weex.requireModule('WeexFunction');
const dom = weex.requireModule("dom");

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('信号记录'));

import SignalRecordCell from "./view/SignalRecordCell";
import VTips from "@/_public/VTips"
export default {

  components: {
    SignalRecordCell,
    VTips
  },
  
  data() {
    return {
      T:T,
      Config: jconfig,

      type:'',
      signalId:'',

      isRefresh:false,
      isLoadinging: false,
      pageMax:false,
      pageNo:1,
      pageSize:20,
      datas:undefined,
    };
  },

  mounted() {
    this.signalId = jnavigator.getParam('signalId');
    this.type = jnavigator.getParam('type');
    this.refresh();
  },
  
  methods: {
    /************************************************* 通信触发*/
    //完全显示
    viewappear() {
    },

    //完全隐藏
    viewdisappear() {
    },

    /************************************************* 其他*/
    refresh() {
      if (!jconfig.isLogin || this.isRefresh) return;
      this.isRefresh = true;
      this.pageNo = 1;
      this.httpsGetSignalList();
    },

    //加载
    loadmore() {
      if (this.pageMax || this.isRefresh || this.isLoadinging) return;
      this.isLoadinging = true;
      this.httpsGetSignalList();
    },

    initDatas(datas) {

      datas.forEach((item)=>{
        item.createTimeStr = jtime.getTime(item.createTime, ' M/d H:m');
      });
      return datas;
    },

    /********************************************** https*/
    //https 信号列表
    httpsGetSignalList(){

      var param = {'type': this.type,
                  'signalId':this.signalId,
                  'pageNo': this.pageNo, 
                  'pageSize': this.pageSize}
      jhttps.post("signal/querySignalRecordList",param,true,(data)=>{
        
        jmodal.hideLoad();
        this.isRefresh = false;
        if(data.status == 200){

          var datas = this.initDatas(data.data);
          if(this.pageNo == 1) {
            this.datas = datas;
          } else {
            this.datas.push(...datas);
          }

          this.pageMax = datas.length < this.pageSize;
          if (this.pageMax && this.datas.length) {
            this.datas.push({});
          }

          this.pageNo++;
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    //https 启动信号
    httpSignalStart(item, server){

      jmodal.showLoad();
      var param = {'signalId': item.signalId, 'address': server.innerIp}
      jhttps.post("signal/signalStart",param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          item.statesBol = true;
          this.$forceUpdate()
          jmodal.toast('启动成功')
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    //https 停止信号
    httpsSignalEnd(item){
      jmodal.showLoad();
      var param = {'signalId': item.signalId}
      jhttps.post("signal/signalEnd",param,true,(data)=>{

        jmodal.hideLoad();
        if(data.status == 200){
          item.statesBol = true;
          this.$forceUpdate()
          jmodal.toast('停止成功')
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    //https 删除信号
    httpsdDeleteSignalById(item, index){

      jmodal.showLoad();
      var param = {'signalId': item.signalId}
      jhttps.post("signal/deleteSignalById",param,true,(data)=>{

        jmodal.hideLoad();
        if(data.status == 200){
          this.datas.splice(index, 1);
          this.datas = this.datas.length==1 ? [] : this.datas;
          jmodal.toast('删除成功')
        }else{
          jmodal.toast(data.msg);
        }
      });
    },
    /********************************** 点击*/
    //功能显示
    funcClick(event, item, index){
      this.$refs.func.show(event.position, this.$refs.content, {item, index});
    },

    //功能选择
    funcSelect(item, index, data) {
      var param = {'signalId':data.item.signalId};
      if (item == '编辑信号') {
        jnavigator.open('', 'signal/SignalEdit', param);
        jbroadcast.subscribeOnce('SignalEdit', ()=>{
          this.refresh();
        });
      }

      if (item == '信号记录') {
        
      }


      if (item == '删除信号') {

        if (data.item.states == 'execute') {
          jmodal.toast('信号启动中无法删除，停止后再删除');
          return;
        }

        var parm = {title:"提示",
                    message:"确认删除信号 "+data.item.signalName+" ？",
                    okBtn:"确认", 
                    cancelBtn:"取消"};
        jmodal.confirm(parm, (bool)=>{
          if (bool == "true") {
            this.httpsdDeleteSignalById(data.item, data.index);
          }
        });
      }
    },

    //启动
    upClick(item, index) {

      jSignal.showServers((server)=>{
        this.httpSignalStart(item, server)
      })
    },

    //停止
    endClick(item, index) {
      this.httpsSignalEnd(item);
    },

    //创建信号
    addClick() {
      if (!jconfig.forceLogin()) return;

      jnavigator.open('', 'signal/SignalAdd');
      jbroadcast.subscribeOnce('SignalAdd', ()=>{
        this.refresh();
      });
    }
  }
}
</script>

<style src="@/_css/style.css" ></style>
<style scoped>
.bg {
  width: 750px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
.list {
  flex: 1;
}
.refresh {
  width: 750;
  flex-direction: row;
  justify-content: center;
}
.load {
  margin-top: 16px;
  height: 40px;
  width: 40px;
}
.genduo {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 750px;
  height: 100px;
}
.tips {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
</style>